package cn.techcave.demo.jpa.service

import cn.techcave.demo.jpa.domain.Record
import cn.techcave.demo.jpa.repository.RecordRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Example
import org.springframework.stereotype.Service
import org.springframework.data.domain.ExampleMatcher
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith


@Service
class RecordService {

    @Autowired
    lateinit var recRepo: RecordRepo
    fun qbeRecord(rec: Record): Record? {
        val matcher = ExampleMatcher.matching()
                .withIgnorePaths("value")//忽略value属性
                .withIncludeNullValues()//包含空值
                .withMatcher("name", startsWith().ignoreCase())//字段name,匹配开始，忽略大小写

        val example = Example.of(rec, matcher)
        return recRepo.findOne(example)
    }

    fun findOne(id:Long): Record? {
        return recRepo.findOne(id)
    }

    fun newRecored(rec: Record) {
        recRepo.save(rec)
    }
}